<html>
<head><meta charset="utf-8"><title>le_bytes and be_bytes functions on slices · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/le_bytes.20and.20be_bytes.20functions.20on.20slices.html">le_bytes and be_bytes functions on slices</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="191834322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/le_bytes%20and%20be_bytes%20functions%20on%20slices/near/191834322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/le_bytes.20and.20be_bytes.20functions.20on.20slices.html#191834322">(Mar 26 2020 at 02:30)</a>:</h4>
<p>Is there any particular reason that there are not versions of <code>from_le_bytes</code> and <code>from_be_bytes</code> that take a slice (<code>&amp;[u8]</code>) as their argument instead of a fixed array? I recently ended up just implementing theses functions myself (and a few others) due to having to constantly write out the boilerplate to extract the required bytes from a slice and convert it into an array. Implementation here for reference: <a href="https://github.com/moonheart08/emutk/blob/master/emutk-core/src/bytes.rs#L2" title="https://github.com/moonheart08/emutk/blob/master/emutk-core/src/bytes.rs#L2">https://github.com/moonheart08/emutk/blob/master/emutk-core/src/bytes.rs#L2</a></p>



<a name="191838466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/le_bytes%20and%20be_bytes%20functions%20on%20slices/near/191838466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/le_bytes.20and.20be_bytes.20functions.20on.20slices.html#191838466">(Mar 26 2020 at 04:04)</a>:</h4>
<p>Well, you can do <code>u32::from_le_bytes(slice.try_into()?)</code> and such</p>



<a name="193017038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/le_bytes%20and%20be_bytes%20functions%20on%20slices/near/193017038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/le_bytes.20and.20be_bytes.20functions.20on.20slices.html#193017038">(Apr 06 2020 at 09:40)</a>:</h4>
<p>Yes, the answer is to combine with <code>try_into</code> as shown in doc examples for <code>from_le_bytes</code> etc.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>